package net.techwatch.fsindex.listener;

import java.io.File;

import net.techwatch.fsindex.Terminable;
import net.techwatch.fsindex.event.FileSystemEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author wiv
 * @since 24/10/2008
 * @version 0.2
 */
public class DeniedOfServiceListener extends FileSystemListenerAdapter {

	private Log log = LogFactory.getLog(DeniedOfServiceListener.class);

	/**
	 * 
	 */
	private String root;

	/**
	 * 
	 */
	private Terminable terminable;

	/**
	 * Default constructor
	 */
	public DeniedOfServiceListener() {
		// nothing to do
	}

	/**
	 * @return the root
	 */
	public String getRoot() {
		return root;
	}

	/**
	 * @param root the root to set
	 */
	public void setRoot(String root) {
		this.root = root;
	}

	/**
	 * @return the fileSystemParser
	 */
	public Terminable getTerminable() {
		return terminable;
	}

	/**
	 * @param fileSystemParser the fileSystemParser to set
	 */
	public void setTerminable(Terminable terminable) {
		this.terminable = terminable;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.curia.pex.objectloader.fs.FileSystemListenerAdapter#onDelete(eu.curia.pex.objectloader.fs.FileSystemEvent)
	 */
	@Override
	public void onDelete(FileSystemEvent event) {
		try {
			if (!new File(root).exists()) {
				log
						.fatal("Denied of service: traduction repository is not available anymore");

				getTerminable().terminate();
			}
		} catch (Exception e) {
			log.fatal("Cannot terminate properly the current process", e);
		}
	}

}
